package cn.wolfcode.cms.web.controller;

import cn.wolfcode.cms.annotation.PermissionName;
import cn.wolfcode.cms.domain.Classification;
import cn.wolfcode.cms.query.QueryObject;
import cn.wolfcode.cms.service.IClassificationService;
import cn.wolfcode.cms.util.JSONResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//分类
@Controller
@RequestMapping("classification")
public class ClassificationController {

    @Autowired
    private IClassificationService classificationService;

    @RequiresPermissions("classification:index")
    @PermissionName("分类首页")
    @RequestMapping("")
    public String index() {
        classificationService.list();
        return "classification/list";
    }

    //分类信息列表/带分页
    @RequiresPermissions("classification:list")
    @PermissionName("分类列表")
    @RequestMapping("list")
    @ResponseBody
    public Object list(QueryObject qo) {
        return classificationService.query(qo);
    }


    @RequestMapping("getClassifyByPsName")
    @ResponseBody
    public Object getClassifyByPsName(String name) {
        return classificationService.getClassifyByPsName(name);
    }

    //分类添加
    @RequiresPermissions("classification:add")
    @PermissionName("分类添加")
    @RequestMapping("save")
    @ResponseBody
    public Object save(Classification entity) {
        JSONResult result = new JSONResult();
        try {
            classificationService.save(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("添加失败");
        }
        return result;
    }

    //分类编辑
    @RequiresPermissions("classification:edit")
    @PermissionName("分类编辑")
    @RequestMapping("edit")
    @ResponseBody
    public Object edit(Classification entity) {
        JSONResult result = new JSONResult();
        try {
            classificationService.update(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("编辑失败");
        }
        return result;
    }

    //分类删除
    @RequiresPermissions("classification:delete")
    @PermissionName("分类删除")
    @RequestMapping("delete")
    @ResponseBody
    public Object delete(Long id) {
        JSONResult result = new JSONResult();
        try {
            classificationService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("删除失败");
        }
        return result;
    }
}
